/** \file gminiGraph.c
* Implementation file for a graph basic structures and traversals
*/
#include "gminiGraph.h"
#include <iostream>
#include <cstdlib>
#include <visitor.h>
using namespace std;


string Node::print()
{
#ifndef COMPACT
	string result="(";
	result+=getNodeType();
	result+=")";
	if (info!="")
	{
		result+="'";
		result+=info;
		result+="'";
	}
	return result;
#else
	return info;
#endif
}

void GminiGraph::deleteSubTree(Node *n)
{
	if (n==NULL)
		return;	
	for (EdgeMap::iterator it=edge_map.find(n);it!=edge_map.end() && (*it).first==n;it++)
		if (it->second->dir==DOWN)	
			deleteSubTree(&*(it->second->end));
	delete n;
}

void Traversal::traverseGraph_helper(Node * startingNode)
{	
	if (startingNode==NULL)
		return;	
	v->visitNode(startingNode);
	for (EdgeMap::iterator it=g->edge_map.find(startingNode);it!=g->edge_map.end() && (*it).first==startingNode;it++)
	{
		if (it->second->dir==DOWN)
		{
			if (it->second->end!=NULL)
			{
				v->visitEdge(it->second);
				traverseGraph_helper(it->second->end);
			}
		}
	}
}

void Traversal::operator()()
{			
	v->initialize();
	traverseGraph_helper(g->getRoot());
	v->finish();
}


